package com.media.photocamera.activity

import android.graphics.Bitmap
import android.graphics.Matrix
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.PopupWindow
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import androidx.camera.core.Preview
import androidx.camera.extensions.*
import com.media.photocamera.utils.CameraUiUtil
import com.media.photocamera.R


open class BaseCameraXActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        this.window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        val lp = this.window.attributes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode =
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
        }
        this.window.attributes = lp
        this.window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        supportActionBar?.hide()
        //设置屏幕亮度最大
        window.attributes.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
    }

    override fun onDestroy() {
        //设置屏幕亮度正常
        window.attributes.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
        super.onDestroy()
    }

    fun showTapView(x: Int, y: Int, previewView: View) {
        val dip40 = CameraUiUtil.dip2px(this, 40)
        val popupWindow = PopupWindow(dip40, dip40)
        val imageView = ImageView(this)
        imageView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        imageView.setBackgroundResource(R.drawable.camerax_shap_focus_tip)
        popupWindow.contentView = imageView
        popupWindow.showAsDropDown(previewView, x - dip40 / 2, y + dip40 / 2)
        previewView.postDelayed({ popupWindow.dismiss() }, 350)
        previewView.playSoundEffect(SoundEffectConstants.CLICK)
    }

    fun setPreviewExtender(builder: Preview.Builder, cameraSelector: CameraSelector) {
        val beautyPreviewExtender = BeautyPreviewExtender.create(builder)
        if (beautyPreviewExtender.isExtensionAvailable(cameraSelector)) {
            // Enable the extension if available.
            beautyPreviewExtender.enableExtension(cameraSelector)
        }

        val hdrPreviewExtender = HdrPreviewExtender.create(builder)
        if (hdrPreviewExtender.isExtensionAvailable(cameraSelector)) {
            hdrPreviewExtender.enableExtension(cameraSelector)
        }

        val nightPreviewExtender = NightPreviewExtender.create(builder)
        if (nightPreviewExtender.isExtensionAvailable(cameraSelector)) {
            nightPreviewExtender.enableExtension(cameraSelector)
        }
    }

    fun setCaptureExtender(builder: ImageCapture.Builder, cameraSelector: CameraSelector) {
        val nightImageCaptureExtender = NightImageCaptureExtender.create(builder)
        if (nightImageCaptureExtender.isExtensionAvailable(cameraSelector)) {
            // Enable the extension if available.
            Log.d("Camera", "night capture extension enable")
            nightImageCaptureExtender.enableExtension(cameraSelector)
        }

        val hdrImageCaptureExtender = HdrImageCaptureExtender.create(builder)
        if (hdrImageCaptureExtender.isExtensionAvailable(cameraSelector)) {
            hdrImageCaptureExtender.enableExtension(cameraSelector)
        }

        val beautyImageCaptureExtender = BeautyImageCaptureExtender.create(builder)
        if (beautyImageCaptureExtender.isExtensionAvailable(cameraSelector)) {
            beautyImageCaptureExtender.enableExtension(cameraSelector)
        }
    }

    fun rotateBitmap(isBack: Boolean, bitmap1: Bitmap): Bitmap {
        try {
            if (bitmap1.width > bitmap1.height) {
                //竖屏旋转照片
                val matrix = Matrix()
                matrix.reset()
                if (isBack) {
                    //   matrix.postScale(-1, 1);
                    //    matrix.preRotate(270);
                    matrix.preRotate(90f)
                } else {
                    matrix.postScale(-1f, 1f)
                    matrix.preRotate(-90f)
                }
                return Bitmap.createBitmap(
                        bitmap1,
                        0,
                        0,
                        bitmap1.width,
                        bitmap1.height,
                        matrix,
                        true
                )
            }
        } catch (e: Exception) {
        }
        return bitmap1
    }

    companion object {
        const val REQUEST_CAMERA = 20
        const val REQUEST_STORAGE = 30

        const val CAPTURED_SAVEING = 1
        const val CAPTURED_IDEL = 2
        const val CAPTURED_SURE = 3
    }

}